import 'package:flutter/material.dart';

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.teal, cardColor: Colors.orange),
      debugShowCheckedModeBanner: false,
      home: HYHomePage(),
    );
  }
}

class HYHomePage extends StatefulWidget {
  @override
  _HYHomePageState createState() => _HYHomePageState();
}

class _HYHomePageState extends State<HYHomePage>
    with SingleTickerProviderStateMixin {
  // 创建AnimationController
  AnimationController _controller;
  Animation _animation;
  Animation _sizeAnimation;

  @override
  void initState() {
    super.initState();

    // 1.创建AnimationController
    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 2),
      // lowerBound: 50,
      // upperBound: 150,
    );

    // 2.设置Curve的值
    _animation = CurvedAnimation(parent: _controller, curve: Curves.linear);

    //3. Tween
    _sizeAnimation = Tween(begin: 50.0, end: 150.0).animate(_animation);

    // 监听动画值的改变
    // _controller.addListener(() {
    //   setState(() {});
    // });

    // 监听动画的状态改变
    _controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        _controller.forward();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    print('执行_HYHomePageState的build方法');
    return Scaffold(
        appBar: AppBar(
          title: Text('Flutter Demo'),
        ),
        body: Center(child: HYAnimatedIcon(_sizeAnimation)),
        backgroundColor: Colors.white,
        floatingActionButton: FloatingActionButton(
            child: Icon(Icons.play_arrow),
            backgroundColor: Colors.teal,
            onPressed: () {
              // _controller.forward();

              if (_controller.isAnimating) {
                _controller.stop();
                print(_controller.status);
              } else if (_controller.status == AnimationStatus.forward) {
                print(_controller.status);
                _controller.forward();
              } else if (_controller.status == AnimationStatus.reverse) {
                _controller.reverse();
              } else {
                _controller.forward();
              }
            }),
        floatingActionButtonLocation: FloatingActionButtonLocation.endFloat);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}

/// AnimatedWidget
///   * 将需要执行的动画的Widget放到一个AnimatedWidget中的build方法里进行返回
///   * 缺点:
///     1.每次都需要创建一个类
///     2.如果构建的Widget有子类，那么子类依然重复构建build
class HYAnimatedIcon extends AnimatedWidget {
  // final Animation _sizeAnimation;
  // HYAnimatedIcon(this._sizeAnimation) : super(listenable: _sizeAnimation);
  HYAnimatedIcon(Animation _sizeAnimation) : super(listenable: _sizeAnimation);

  @override
  Widget build(BuildContext context) {
    Animation animation = listenable;
    return Icon(Icons.favorite, color: Colors.red, size: animation.value);
    // return Icon(Icons.favorite, color: Colors.red, size: _sizeAnimation.value);
  }
}
